﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace FPC.Offer.UI.GeneralProcessor.SkyDriveHand
{
    /// <summary>
    /// DeleteHandler 的摘要说明
    /// </summary>
    public class DeleteHandler : IHttpHandler
    {
        FPC.Offer.Service.System_NetTrayTableService Sky = new Service.System_NetTrayTableService();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            var id = Convert.ToInt32(context.Request["id"]);

            var cid = context.Request["ids"];
            if (id > 0)
            {

                Model.Dropbox_NetTray c = Sky.Find(w => w.NeiTrayId == id).FirstOrDefault();

                int a = Sky.Delete(c);
                if (a > 0)
                {

                    string upath = c.NUrl;
                    if (Directory.Exists(upath))
                    {
                        Directory.Delete(upath);
                        context.Response.Write(1);
                    }
                    else
                    {
                        return;
                    }


                }
                else
                {
                    context.Response.Write(0);

                }

            }

            else
            {
                string[] strarr = cid.Split(',');
                int count = 0;

                for (int i = 0; i < strarr.Length; i++)
                {
                    int did = int.Parse(strarr[i]);
                    Model.Dropbox_NetTray d = Sky.Find(w => w.NeiTrayId == did).FirstOrDefault();
                    string path = d.NUrl;
                    int r = Sky.Delete(d);
                    if (r > 0)
                    {
                        if (Directory.Exists(path))
                        {
                            Directory.Delete(path);
                            count++;

                        }
                        else
                        {
                            return;
                        }

                    }
                    else
                    {

                        context.Response.Write(0);
                        return;
                    }



                }
                context.Response.Write(1);
                return;

            }


        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}